package com.cosin.dao.mysql;

import com.cosin.model.po.UserPO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface UserMapper {

    @Insert("insert into t_user(name,password,email,role)" +
            "values (#{username},#{password},#{email},#{role})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertUser(UserPO userPO);

    @Select("select * from t_user where name=#{username}")
    @Results(id="userMap",value = {
            @Result(column = "name", property = "username"),
    })
    UserPO getUserByUsername(String username);

    /**
     * 得到所有角色信息
     */
    @Select("select * from t_user")
    @Results(id = "getAllUser", value = {
            @Result(column = "name", property = "username"),
    })
    List<UserPO> getAllUser();

    /**
     * 更改角色信息
     */
    @Update("update t_user set name=#{username},email=#{email},role=#{role} where id=#{id}")
    @Results(id = "updateUser", value = {
            @Result(column = "name", property = "username"),
    })
    int updateUser(UserPO userPO);


    /**
     * 更改信息 附加密码
     */
    @Update("update t_user set name=#{username},email=#{email},password=#{password},role=#{role} where id=#{id}")
    @Results(id = "updateUserWithPassword", value = {
            @Result(column = "name", property = "username"),
    })
    int updateUserWithPassword(UserPO userPO);

    @Update("update t_user set password=#{password} where id=#{id}")
    @Results(id = "resetUserPassword", value = {
            @Result(column = "name", property = "username"),
    })
    int resetUserPassword(UserPO userPO);

    /**
     * 根据id获取用户信息
     * @param userId
     * @return
     */
    @Select("select * from t_user where id=#{userId}")
    @Results(id = "getUserById", value = {
            @Result(column = "name", property = "username"),
    })
    UserPO getUserById(int userId);

}
